<template>
	<view>
		<view class="search-box">
			<my-search @click="goSearch"></my-search>
		</view>

		<swiper autoplay indicator-dots :interval="3000" circular="true">
			<swiper-item v-for="item in banners" :key="item.goods_id">
				<view class="item-pic">
					<image :src="item.image_src" mode="widthFix" @click="goDetail(item.goods_id)"></image>
				</view>
			</swiper-item>
		</swiper>

		<view class="nav-list">
			<view v-for="(item,index) in cates" :key="index">
				<image :src="item.image_src" class="nav-img" @click="goCates(item)"></image>
			</view>
		</view>

		<view class="floor-list">
			<view v-for="(item,index) in floors" :key="index">
				<image :src="item.floor_title.image_src" class="floor-title"></image>
				<!-- 标题下图片区域 -->
				<view class="floor-img-box">
					<!-- 左侧大图片的盒子 -->
					<navigator url="/subpkg/goods_list/goods_list" class="left-img-box">
						<image class="left-img" :src="item.product_list[0].image_src"
							:style="{width:item.product_list[0].image_width+'rpx'}"></image>
					</navigator>
					<!-- 右侧 4 个小图片的盒子 -->
					<view class="right-img-box">
						<navigator url="/subpkg/goods_list/goods_list" class="right-img-item"
							v-for="(item1,i2) in item.product_list" :key="i2" v-if="i2 !== 0">
							<image :src="item1.image_src" mode="widthFix" :style="{width:item1.image_width+'rpx'}">
							</image>
						</navigator>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		getBanners,
		getCates,
		getFloors
	} from '../../api/home';
	import toast from '@/utils/toast.js'
	import tabbarMix from '@/mixins/tabbar.js'
	export default {
		mixins: [tabbarMix],
		data() {
			return {
				banners: [],
				cates: [],
				floors: []
			};
		},
		onLoad() {
			this.getBanners()
			this.getCates()
			this.getFloors()
		},
		methods: {
			async getBanners() {
				try {
					const res = await getBanners()
					// console.log(res)
					this.banners = res.message
					toast.success({
						title: '请求成功'
					})
				} catch (e) {
					//TODO handle the exception
				}
			},
			async getCates() {
				const res = await getCates()
				// console.log(res)
				this.cates = res.message
			},
			async getFloors() {
				const res = await getFloors()
				console.log(res)
				this.floors = res.message
			},
			goDetail(id) {
				uni.navigateTo({
					url: '/subpkg/goods_detail/goods_detail?goods_id=' + id
				})
			},
			goCates(item) {
				if (item.name === '分类') {
					uni.switchTab({
						url: '/pages/cate/cate'
					})
				}
			},
			goSearch() {
				uni.navigateTo({
					url: '/subpkg/goods_search/goods_search'
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.item-pic {
		width: 750rpx;

		image {
			width: 100%;
		}
	}

	.nav-list {
		display: flex;
		justify-content: space-around;
		margin: 15px 0;

		.nav-img {
			width: 128rpx;
			height: 140rpx;
		}
	}

	.floor-title {
		height: 60rpx;
		width: 100%;
		display: flex;
	}

	.right-img-box {
		display: flex;
		flex-wrap: wrap;
		justify-content: space-around;
	}

	.floor-img-box {
		display: flex;
		padding-left: 10rpx;

		.left-img {
			height: 392rpx !important;
		}
	}

	.search-box {
		position: sticky;
		top: 0;
		z-index: 999;
	}
</style>
